const requireEx=require('yi-require');
const lookup = require('yi-lookup');

const beforeSend = function (req,body,callback) {
    let stockList = (body.data instanceof Array)?body.data:body.data.items;

    let lookupList = [];
    lookupList.push({
        from:"goods",
        localField:"goodsId",
        foreignField:"_id",
        project:{createTime:0,lastModifyDate:0,pwd:0}
    }) ;

    lookup.lookup(req.mongo,stockList,lookupList,(e,r)=> {
        goodsList = r;
        callback(null, body);
    });
};


middleware =  (req, res, next) =>{
    req.beforeSend = beforeSend;
    next();
};

module.exports = {
    open:true,
    description:'获取商品库存列表',
    extend:"",
    middleware:middleware,
    handler: requireEx('yi-restful').r('stock')
};
